﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using CoreMVVM.Commands;
using CoreMVVM.Services;
using CoreMVVM.ViewModels;
using CoreMVVM.Messenger;

namespace WpfModelViewApplication2.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private DelegateCommand exitCommand;
        private DelegateCommand openFileCommand;
        private DelegateCommand openWindow1Command;
        private DelegateCommand<TextChangedEventArgs> textChangedCommand;

        private IOpenFileService openFileService = null;
        private IUIVisualizerService uiVisualService = null;
        private MessageMediator messageMediator = null;

        #region Constructor

        public MainViewModel()
        {
            //By default, registering the service over again doesnt overwrite the Service - you need to pass the bool parameter to the RegisterService call
            ViewModelBase.ServiceProvider.RegisterService<IOpenFileService>(new OpenFileService());
            openFileService = GetService<IOpenFileService>();

            ViewModelBase.ServiceProvider.RegisterService<IUIVisualizerService>(new UIVisualizerService());
            uiVisualService = GetService<IUIVisualizerService>();

            // The messageMediator is registered in the ViewModelBase - Generally you have 1 mediator; Hence, the restricted access to the constructor
            messageMediator = GetService<MessageMediator>();
        }

        #endregion

        #region TextChanged

        public ICommand TextChangedCommand
        {
            get
            {
                if (textChangedCommand == null)
                {
                    textChangedCommand = new DelegateCommand<TextChangedEventArgs>(OnTextChanged);
                }
                return textChangedCommand;
            }
        }

        private void OnTextChanged(TextChangedEventArgs e)
        {
            string text = (e.Source as TextBox).Text;
            Application.Current.MainWindow.Title = text;

        }

        #endregion

        #region OpenFileCommand

        public ICommand OpenFileCommand
        {
            get
            {
                if (openFileCommand == null)
                {
                    openFileCommand = new DelegateCommand(OpenFile);
                }
                return openFileCommand;
            }
        }

        private void OpenFile()
        {
            bool? result = openFileService.ShowDialog(null);
            if (result.HasValue && result.Value)
            {
                Application.Current.MainWindow.Title = openFileService.OpenedFileName;
                GetService<MessageMediator>().BroadcaseMessage(MessageSilo.FileSelected, openFileService.OpenedFileName);
            }
        }

        #endregion

        #region OpenWindow1Command

        public ICommand OpenWindow1Command
        {
            get
            {
                if (openWindow1Command == null)
                {
                    openWindow1Command = new DelegateCommand(OpenWindow1);
                }
                return openWindow1Command;
            }
        }

        private void OpenWindow1()
        {
            //bool? result = uiVisualService.ShowDialog("SimpleWindow1", new Window1ViewModel());
            uiVisualService.Show("SimpleWindow1", new Window1ViewModel(), true, null);

        }

        #endregion

        

        #region ExitCommand

        public ICommand ExitCommand
        {
            get
            {
                if (exitCommand == null)
                {
                    exitCommand = new DelegateCommand(Exit);
                }
                return exitCommand;
            }
        }

        private void Exit()
        {
            Application.Current.Shutdown();
        }

        #endregion


    }
}
